/**
 * amis v2.4.0
 * Copyright 2018-2022 baidu
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var amisUi = require('amis-ui');
var omit = require('lodash/omit');
var findIndex = require('lodash/findIndex');
var cloneDeep = require('lodash/cloneDeep');
var isEqual = require('lodash/isEqual');
var inRange = require('lodash/inRange');
var find = require('lodash/find');
var moment = require('moment');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
var findIndex__default = /*#__PURE__*/_interopDefaultLegacy(findIndex);
var cloneDeep__default = /*#__PURE__*/_interopDefaultLegacy(cloneDeep);
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
var inRange__default = /*#__PURE__*/_interopDefaultLegacy(inRange);
var find__default = /*#__PURE__*/_interopDefaultLegacy(find);
var moment__default = /*#__PURE__*/_interopDefaultLegacy(moment);

var FormTable = /** @class */ (function (_super) {
    tslib.__extends(FormTable, _super);
    function FormTable(props) {
        var _this = _super.call(this, props) || this;
        _this.entityId = 1;
        _this.subForms = {};
        _this.rowPrinstine = [];
        _this.editting = {};
        _this.state = {
            columns: _this.buildColumns(props),
            editIndex: -1,
            items: Array.isArray(props.value) ? props.value.concat() : []
        };
        _this.entries = new amisCore.SimpleMap();
        _this.buildItemProps = _this.buildItemProps.bind(_this);
        _this.confirmEdit = _this.confirmEdit.bind(_this);
        _this.cancelEdit = _this.cancelEdit.bind(_this);
        _this.handleSaveTableOrder = _this.handleSaveTableOrder.bind(_this);
        _this.handleTableSave = _this.handleTableSave.bind(_this);
        _this.getEntryId = _this.getEntryId.bind(_this);
        _this.subFormRef = _this.subFormRef.bind(_this);
        _this.handlePageChange = _this.handlePageChange.bind(_this);
        _this.emitValue = _this.emitValue.bind(_this);
        return _this;
    }
    FormTable.prototype.componentDidUpdate = function (nextProps) {
        var props = this.props;
        var toUpdate = null;
        if (props.columns !== nextProps.columns) {
            toUpdate = tslib.__assign(tslib.__assign({}, toUpdate), { columns: this.buildColumns(props) });
        }
        if (props.value !== nextProps.value) {
            toUpdate = tslib.__assign(tslib.__assign({}, toUpdate), { items: Array.isArray(props.value) ? props.value.concat() : [], editIndex: -1, raw: undefined });
        }
        toUpdate && this.setState(toUpdate);
    };
    FormTable.prototype.componentWillUnmount = function () {
        this.entries.dispose();
    };
    FormTable.prototype.subFormRef = function (form, x, y) {
        this.subForms["".concat(x, "-").concat(y)] = form;
    };
    FormTable.prototype.validate = function () {
        return tslib.__awaiter(this, void 0, void 0, function () {
            var _a, value, minLength, maxLength, __, columns, subForms_1, results_1, msg, uniqueColumn_1, children;
            var _this = this;
            return tslib.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, value = _a.value, minLength = _a.minLength, maxLength = _a.maxLength, __ = _a.translate, columns = _a.columns;
                        // todo: 如果当前正在编辑中，表单提交了，应该先让正在编辑的东西提交然后再做验证。
                        if (~this.state.editIndex) {
                            return [2 /*return*/, __('Table.editing')];
                        }
                        if (!(minLength && (!Array.isArray(value) || value.length < minLength))) return [3 /*break*/, 1];
                        return [2 /*return*/, __('Combo.minLength', { minLength: minLength })];
                    case 1:
                        if (!(maxLength && Array.isArray(value) && value.length > maxLength)) return [3 /*break*/, 2];
                        return [2 /*return*/, __('Combo.maxLength', { maxLength: maxLength })];
                    case 2:
                        subForms_1 = [];
                        Object.keys(this.subForms).forEach(function (key) { return _this.subForms[key] && subForms_1.push(_this.subForms[key]); });
                        if (!subForms_1.length) return [3 /*break*/, 4];
                        return [4 /*yield*/, Promise.all(subForms_1.map(function (item) { return item.validate(); }))];
                    case 3:
                        results_1 = _b.sent();
                        msg = ~results_1.indexOf(false) ? __('Form.validateFailed') : '';
                        uniqueColumn_1 = '';
                        if (!msg &&
                            Array.isArray(columns) &&
                            Array.isArray(value) &&
                            columns.some(function (item) {
                                if (item.unique && item.name) {
                                    var exists_1 = [];
                                    return value.some(function (obj) {
                                        var value = amisCore.getVariable(obj, item.name);
                                        if (~exists_1.indexOf(value)) {
                                            uniqueColumn_1 = "".concat(item.label || item.name);
                                            return true;
                                        }
                                        exists_1.push(value);
                                        return false;
                                    });
                                }
                                return false;
                            })) {
                            msg = __('InputTable.uniqueError', {
                                label: uniqueColumn_1
                            });
                        }
                        return [2 /*return*/, msg];
                    case 4:
                        if (!this.tableStore)
                            return [2 /*return*/];
                        children = this.tableStore.children.filter(function (item) { return (item === null || item === void 0 ? void 0 : item.storeType) === 'FormItemStore'; });
                        return [4 /*yield*/, Promise.all(children.map(function (item) { return item.validate(_this.props.value); }))];
                    case 5:
                        _b.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    FormTable.prototype.emitValue = function () {
        var items = this.state.items.filter(function (item) { return !item.__isPlaceholder; });
        var onChange = this.props.onChange;
        onChange === null || onChange === void 0 ? void 0 : onChange(items);
    };
    FormTable.prototype.doAction = function (action, ctx) {
        var rest = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            rest[_i - 2] = arguments[_i];
        }
        return tslib.__awaiter(this, void 0, void 0, function () {
            var _a, onAction, valueField, env, needConfirm, addable, addApi, __, items_1, toAdd_1, payload, items_2, toRemove;
            var _this = this;
            return tslib.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, onAction = _a.onAction, valueField = _a.valueField, env = _a.env, _a.onChange, _a.editable, needConfirm = _a.needConfirm, addable = _a.addable, addApi = _a.addApi, __ = _a.translate;
                        if (!(action.actionType === 'add')) return [3 /*break*/, 6];
                        if (addable === false) {
                            return [2 /*return*/];
                        }
                        items_1 = this.state.items.concat();
                        if (!(addApi || action.payload)) return [3 /*break*/, 4];
                        toAdd_1 = null;
                        if (!amisCore.isEffectiveApi(addApi, ctx)) return [3 /*break*/, 2];
                        return [4 /*yield*/, env.fetcher(addApi, ctx)];
                    case 1:
                        payload = _b.sent();
                        if (payload && !payload.ok) {
                            env.notify('error', payload.msg || __('fetchFailed'));
                            return [2 /*return*/];
                        }
                        else if (payload && payload.ok) {
                            toAdd_1 = payload.data;
                        }
                        return [3 /*break*/, 3];
                    case 2:
                        toAdd_1 = amisCore.dataMapping(action.payload, ctx);
                        _b.label = 3;
                    case 3:
                        toAdd_1 = Array.isArray(toAdd_1) ? toAdd_1 : [toAdd_1];
                        toAdd_1.forEach(function (toAdd) {
                            if (!valueField ||
                                !find__default["default"](items_1, function (item) { return item[valueField] == toAdd[valueField]; })) {
                                // 不要重复加入
                                items_1.push(toAdd);
                            }
                        });
                        this.setState({
                            items: items_1
                        }, function () {
                            _this.emitValue();
                            if (toAdd_1.length === 1 && needConfirm !== false) {
                                _this.startEdit(items_1.length - 1, true);
                            }
                        });
                        return [2 /*return*/];
                    case 4: return [2 /*return*/, this.addItem(items_1.length - 1)];
                    case 5: return [3 /*break*/, 7];
                    case 6:
                        if (action.actionType === 'remove' ||
                            action.actionType === 'delete') {
                            if (!valueField) {
                                return [2 /*return*/, env.alert(__('Table.valueField'))];
                            }
                            else if (!action.payload) {
                                return [2 /*return*/, env.alert(__('Table.playload'))];
                            }
                            items_2 = this.state.items.concat();
                            toRemove = amisCore.dataMapping(action.payload, ctx);
                            toRemove = Array.isArray(toRemove) ? toRemove : [toRemove];
                            toRemove.forEach(function (toRemove) {
                                var idx = findIndex__default["default"](items_2, function (item) { return item[valueField] == toRemove[valueField]; });
                                if (~idx) {
                                    items_2.splice(idx, 1);
                                }
                            });
                            this.setState({
                                items: items_2
                            }, function () { return _this.emitValue(); });
                            // todo 如果配置删除 Api 怎么办？
                            return [2 /*return*/];
                        }
                        _b.label = 7;
                    case 7: return [2 /*return*/, onAction && onAction.apply(void 0, tslib.__spreadArray([action, ctx], rest, false))];
                }
            });
        });
    };
    FormTable.prototype.copyItem = function (index) {
        var _this = this;
        var needConfirm = this.props.needConfirm;
        var items = this.state.items.concat();
        items.splice(index + 1, 0, items[index]);
        index = Math.min(index + 1, items.length - 1);
        this.setState({
            items: items
        }, function () {
            if (needConfirm === false) {
                _this.emitValue();
            }
            else {
                _this.startEdit(index, true);
            }
        });
    };
    FormTable.prototype.addItem = function (index) {
        var _this = this;
        var _a = this.props, needConfirm = _a.needConfirm, scaffold = _a.scaffold, columns = _a.columns, data = _a.data;
        var items = this.state.items.concat();
        var value = {
            __isPlaceholder: true
        };
        if (Array.isArray(columns)) {
            columns.forEach(function (column) {
                if (typeof column.value !== 'undefined' &&
                    typeof column.name === 'string') {
                    if ('type' in column &&
                        (column.type === 'input-date' ||
                            column.type === 'input-datetime' ||
                            column.type === 'input-time' ||
                            column.type === 'input-month' ||
                            column.type === 'input-quarter' ||
                            column.type === 'input-year')) {
                        var date = amisCore.filterDate(column.value, data, column.format || 'X');
                        amisCore.setVariable(value, column.name, (column.utc ? moment__default["default"].utc(date) : date).format(column.format || 'X'));
                    }
                    else {
                        /** 如果value值设置为表达式，则忽略 */
                        if (!amisCore.isExpression(column.value)) {
                            amisCore.setVariable(value, column.name, column.value);
                        }
                    }
                }
            });
        }
        value = tslib.__assign(tslib.__assign({}, value), scaffold);
        if (needConfirm === false) {
            delete value.__isPlaceholder;
        }
        items.splice(index + 1, 0, value);
        index = Math.min(index + 1, items.length - 1);
        this.setState({
            items: items
        }, function () {
            if (needConfirm === false) {
                _this.emitValue();
            }
            else {
                _this.startEdit(index, true);
            }
        });
    };
    FormTable.prototype.startEdit = function (index, isCreate) {
        if (isCreate === void 0) { isCreate = false; }
        this.setState({
            editIndex: index,
            isCreateMode: isCreate,
            raw: this.state.items[index],
            columns: this.buildColumns(this.props, isCreate)
        });
    };
    FormTable.prototype.confirmEdit = function () {
        return tslib.__awaiter(this, void 0, void 0, function () {
            var _a, addApi, updateApi, data, env, __, subForms, items, item, isNew, remote;
            var _this = this;
            return tslib.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, addApi = _a.addApi, updateApi = _a.updateApi, data = _a.data, env = _a.env, __ = _a.translate;
                        subForms = [];
                        Object.keys(this.subForms).forEach(function (key) { return _this.subForms[key] && subForms.push(_this.subForms[key]); });
                        subForms.forEach(function (form) { return form.flush(); });
                        items = this.state.items.concat();
                        item = tslib.__assign({}, items[this.state.editIndex]);
                        isNew = item.__isPlaceholder;
                        remote = null;
                        if (!(isNew && amisCore.isEffectiveApi(addApi, amisCore.createObject(data, item)))) return [3 /*break*/, 2];
                        return [4 /*yield*/, env.fetcher(addApi, amisCore.createObject(data, item))];
                    case 1:
                        remote = _b.sent();
                        return [3 /*break*/, 4];
                    case 2:
                        if (!amisCore.isEffectiveApi(updateApi, amisCore.createObject(data, item))) return [3 /*break*/, 4];
                        return [4 /*yield*/, env.fetcher(updateApi, amisCore.createObject(data, item))];
                    case 3:
                        remote = _b.sent();
                        _b.label = 4;
                    case 4:
                        if (remote && !remote.ok) {
                            env.notify('error', remote.msg || __('saveFailed'));
                            return [2 /*return*/];
                        }
                        else if (remote && remote.ok) {
                            item = tslib.__assign(tslib.__assign({}, ((isNew ? addApi : updateApi).replaceData
                                ? {}
                                : item)), remote.data);
                        }
                        delete item.__isPlaceholder;
                        items.splice(this.state.editIndex, 1, item);
                        this.setState({
                            editIndex: -1,
                            items: items,
                            raw: undefined
                        }, this.emitValue);
                        return [2 /*return*/];
                }
            });
        });
    };
    FormTable.prototype.cancelEdit = function () {
        var items = this.state.items.concat();
        if (this.state.isCreateMode) {
            items = items.filter(function (item) { return !item.__isPlaceholder; });
        }
        else if (this.state.raw) {
            items.splice(this.state.editIndex, 1, this.state.raw);
        }
        this.setState({
            editIndex: -1,
            raw: undefined,
            items: items
        }, this.emitValue);
    };
    FormTable.prototype.removeItem = function (index) {
        return tslib.__awaiter(this, void 0, void 0, function () {
            var _a, value, onChange, deleteApi, deleteConfirmText, env, data, __, newValue, item, ctx, confirmed, result;
            return tslib.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, value = _a.value, onChange = _a.onChange, deleteApi = _a.deleteApi, deleteConfirmText = _a.deleteConfirmText, env = _a.env, data = _a.data, __ = _a.translate;
                        newValue = Array.isArray(value) ? value.concat() : [];
                        item = newValue[index];
                        if (!item) {
                            return [2 /*return*/];
                        }
                        ctx = amisCore.createObject(data, item);
                        if (!amisCore.isEffectiveApi(deleteApi, ctx)) return [3 /*break*/, 3];
                        return [4 /*yield*/, env.confirm(deleteConfirmText ? amisCore.filter(deleteConfirmText, ctx) : __('deleteConfirm'))];
                    case 1:
                        confirmed = _b.sent();
                        if (!confirmed) {
                            // 如果不确认，则跳过！
                            return [2 /*return*/];
                        }
                        return [4 /*yield*/, env.fetcher(deleteApi, ctx)];
                    case 2:
                        result = _b.sent();
                        if (!result.ok) {
                            env.notify('error', __('deleteFailed'));
                            return [2 /*return*/];
                        }
                        _b.label = 3;
                    case 3:
                        this.removeEntry(item);
                        newValue.splice(index, 1);
                        onChange(newValue);
                        return [2 /*return*/];
                }
            });
        });
    };
    FormTable.prototype.buildItemProps = function (item, index) {
        if (this.props.needConfirm === false) {
            return {
                quickEditEnabled: true
            };
        }
        else if (!this.props.editable &&
            !this.props.addable &&
            !this.state.isCreateMode) {
            return null;
        }
        var perPage = this.props.perPage;
        var page = this.state.page || 1;
        var offset = 0;
        if (typeof perPage === 'number' && perPage) {
            offset = (page - 1) * perPage;
        }
        return {
            quickEditEnabled: this.state.editIndex === index + offset
        };
    };
    FormTable.prototype.buildColumns = function (props, isCreateMode) {
        var _this = this;
        if (isCreateMode === void 0) { isCreateMode = false; }
        var env = this.props.env;
        var columns = Array.isArray(props.columns)
            ? props.columns.concat()
            : [];
        var ns = this.props.classPrefix;
        var __ = this.props.translate;
        var needConfirm = this.props.needConfirm;
        var showIndex = this.props.showIndex;
        var btns = [];
        if (props.addable && props.showAddBtn !== false) {
            btns.push({
                children: function (_a) {
                    var key = _a.key, rowIndex = _a.rowIndex, offset = _a.offset;
                    return ~_this.state.editIndex && needConfirm !== false ? null : (React__default["default"].createElement(amisUi.Button, { classPrefix: ns, size: "sm", key: key, level: "link", tooltip: __('Table.addRow'), tooltipContainer: env && env.getModalContainer ? env.getModalContainer : undefined, onClick: _this.addItem.bind(_this, rowIndex + offset, undefined) },
                        props.addBtnLabel ? React__default["default"].createElement("span", null, props.addBtnLabel) : null,
                        props.addBtnIcon ? (typeof props.addBtnIcon === 'string' ? (React__default["default"].createElement(amisUi.Icon, { icon: props.addBtnIcon, className: "icon" })) : (amisCore.generateIcon(props.classnames, props.addBtnIcon))) : null));
                }
            });
        }
        if (props.copyable && props.showCopyBtn !== false) {
            btns.push({
                children: function (_a) {
                    var key = _a.key, rowIndex = _a.rowIndex, offset = _a.offset;
                    return ~_this.state.editIndex && needConfirm !== false ? null : (React__default["default"].createElement(amisUi.Button, { classPrefix: ns, size: "sm", key: key, level: "link", tooltip: __('Table.copyRow'), tooltipContainer: env && env.getModalContainer ? env.getModalContainer : undefined, onClick: _this.copyItem.bind(_this, rowIndex + offset, undefined) },
                        props.copyBtnLabel ? React__default["default"].createElement("span", null, props.copyBtnLabel) : null,
                        props.copyBtnIcon ? (typeof props.copyBtnIcon === 'string' ? (React__default["default"].createElement(amisUi.Icon, { icon: props.copyBtnIcon, className: "icon" })) : (amisCore.generateIcon(props.classnames, props.copyBtnIcon))) : null));
                }
            });
        }
        if (props.needConfirm === false) {
            columns = columns.map(function (column) {
                var quickEdit = column.quickEdit;
                return quickEdit === false
                    ? omit__default["default"](column, ['quickEdit'])
                    : tslib.__assign(tslib.__assign({}, column), { quickEdit: tslib.__assign(tslib.__assign(tslib.__assign({}, _this.columnToQuickEdit(column)), quickEdit), { saveImmediately: true, mode: 'inline' }) });
            });
        }
        else if (props.addable || props.editable || isCreateMode) {
            columns = columns.map(function (column) {
                var quickEdit = !isCreateMode && column.hasOwnProperty('quickEditOnUpdate')
                    ? column.quickEditOnUpdate
                    : column.quickEdit;
                return quickEdit === false
                    ? omit__default["default"](column, ['quickEdit'])
                    : tslib.__assign(tslib.__assign({}, column), { quickEdit: tslib.__assign(tslib.__assign(tslib.__assign({}, _this.columnToQuickEdit(column)), quickEdit), { saveImmediately: true, mode: 'inline' }) });
            });
            props.editable &&
                btns.push({
                    children: function (_a) {
                        var key = _a.key, rowIndex = _a.rowIndex, data = _a.data, offset = _a.offset;
                        return ~_this.state.editIndex || (data && data.__isPlaceholder) ? null : (React__default["default"].createElement(amisUi.Button, { classPrefix: ns, size: "sm", key: key, level: "link", tooltip: __('Table.editRow'), tooltipContainer: env && env.getModalContainer
                                ? env.getModalContainer
                                : undefined, onClick: function () { return _this.startEdit(rowIndex + offset); } },
                            props.updateBtnLabel || props.editBtnLabel ? (React__default["default"].createElement("span", null, props.updateBtnLabel || props.editBtnLabel)) : null,
                            typeof props.updateBtnIcon !== 'undefined' ? (props.updateBtnIcon ? (typeof props.updateBtnIcon === 'string' ? (React__default["default"].createElement(amisUi.Icon, { icon: props.updateBtnIcon, className: "icon" })) : (amisCore.generateIcon(props.classnames, props.updateBtnIcon))) : null) : props.editBtnIcon ? (typeof props.editBtnIcon === 'string' ? (React__default["default"].createElement(amisUi.Icon, { icon: props.editBtnIcon, className: "icon" })) : (amisCore.generateIcon(props.classnames, props.editBtnIcon))) : null));
                    }
                });
            btns.push({
                children: function (_a) {
                    var key = _a.key, rowIndex = _a.rowIndex, offset = _a.offset;
                    return _this.state.editIndex === rowIndex + offset ? (React__default["default"].createElement(amisUi.Button, { classPrefix: ns, size: "sm", key: key, level: "link", tooltip: __('save'), tooltipContainer: env && env.getModalContainer ? env.getModalContainer : undefined, onClick: _this.confirmEdit },
                        props.confirmBtnLabel ? (React__default["default"].createElement("span", null, props.confirmBtnLabel)) : null,
                        props.confirmBtnIcon ? (typeof props.confirmBtnIcon === 'string' ? (React__default["default"].createElement(amisUi.Icon, { icon: props.confirmBtnIcon, className: "icon" })) : (amisCore.generateIcon(props.classnames, props.confirmBtnIcon))) : null)) : null;
                }
            });
            btns.push({
                children: function (_a) {
                    var key = _a.key, rowIndex = _a.rowIndex, offset = _a.offset;
                    return _this.state.editIndex === rowIndex + offset ? (React__default["default"].createElement(amisUi.Button, { classPrefix: ns, size: "sm", key: key, level: "link", tooltip: __('cancel'), tooltipContainer: env && env.getModalContainer ? env.getModalContainer : undefined, onClick: _this.cancelEdit },
                        props.cancelBtnLabel ? (React__default["default"].createElement("span", null, props.cancelBtnLabel)) : null,
                        props.cancelBtnIcon ? (typeof props.cancelBtnIcon === 'string' ? (React__default["default"].createElement(amisUi.Icon, { icon: props.cancelBtnIcon, className: "icon" })) : (amisCore.generateIcon(props.classnames, props.cancelBtnIcon))) : null)) : null;
                }
            });
        }
        if (props.removable) {
            btns.push({
                children: function (_a) {
                    var key = _a.key, rowIndex = _a.rowIndex, data = _a.data, offset = _a.offset;
                    return (~_this.state.editIndex || (data && data.__isPlaceholder)) &&
                        needConfirm !== false ? null : (React__default["default"].createElement(amisUi.Button, { classPrefix: ns, size: "sm", key: key, level: "link", tooltip: __('Table.deleteRow'), tooltipContainer: env && env.getModalContainer ? env.getModalContainer : undefined, onClick: _this.removeItem.bind(_this, rowIndex + offset) },
                        props.deleteBtnLabel ? (React__default["default"].createElement("span", null, props.deleteBtnLabel)) : null,
                        props.deleteBtnIcon ? (typeof props.deleteBtnIcon === 'string' ? (React__default["default"].createElement(amisUi.Icon, { icon: props.deleteBtnIcon, className: "icon" })) : (amisCore.generateIcon(props.classnames, props.deleteBtnIcon))) : null));
                }
            });
        }
        if (btns.length) {
            var operation = columns.find(function (item) { return item.type === 'operation'; });
            if (!operation) {
                operation = {
                    type: 'operation',
                    buttons: [],
                    label: __('Table.operation'),
                    className: 'v-middle nowrap',
                    fixed: 'right',
                    width: '1%',
                    innerClassName: 'm-n'
                };
                columns.push(operation);
            }
            operation.buttons = Array.isArray(operation.buttons)
                ? operation.buttons.concat()
                : [];
            operation.buttons.unshift.apply(operation.buttons, btns);
        }
        if (showIndex) {
            columns.unshift({
                label: __('Table.index'),
                width: '1%',
                children: function (props) {
                    return React__default["default"].createElement("td", null, props.offset + props.data.index + 1);
                }
            });
        }
        return columns;
    };
    FormTable.prototype.columnToQuickEdit = function (column) {
        var quickEdit = {
            type: 'input-text'
        };
        if ((column.type &&
            /^input\-|(?:select|picker|checkbox|checkboxes|editor|transfer|radios)$/i.test(column.type)) ||
            ~['textarea', 'combo', 'condition-builder', 'group'].indexOf(column.type)) {
            return tslib.__assign(tslib.__assign({}, column), { label: '' });
        }
        return quickEdit;
    };
    FormTable.prototype.handleTableSave = function (rows, diff, rowIndexes) {
        var perPage = this.props.perPage;
        if (~this.state.editIndex) {
            var items_3 = this.state.items.concat();
            var origin_1 = items_3[this.state.editIndex];
            if (!origin_1) {
                return;
            }
            var value = tslib.__assign({}, rows);
            this.entries.set(value, this.entries.get(origin_1) || this.entityId++);
            this.entries.delete(origin_1);
            items_3.splice(this.state.editIndex, 1, value);
            this.setState({
                items: items_3
            });
            return;
        }
        var page = this.state.page;
        var items = this.state.items.concat();
        if (Array.isArray(rows)) {
            rowIndexes.forEach(function (rowIndex, index) {
                var indexes = rowIndex.split('.').map(function (item) { return parseInt(item, 10); });
                if (page && page > 1 && typeof perPage === 'number') {
                    indexes[0] += (page - 1) * perPage;
                }
                var origin = amisCore.getTree(items, indexes);
                var data = tslib.__assign(tslib.__assign({}, origin), diff[index]);
                items = amisCore.spliceTree(items, indexes, 1, data);
            });
        }
        else {
            var indexes = rowIndexes
                .split('.')
                .map(function (item) { return parseInt(item, 10); });
            if (page && page > 1 && typeof perPage === 'number') {
                indexes[0] += (page - 1) * perPage;
            }
            var origin_2 = amisCore.getTree(items, indexes);
            var data = tslib.__assign(tslib.__assign({}, origin_2), diff);
            items = amisCore.spliceTree(items, indexes, 1, data);
            this.entries.set(data, this.entries.get(origin_2) || this.entityId++);
            // this.entries.delete(origin); // 反正最后都会清理的，先不删了吧。
        }
        this.setState({
            items: items
        }, this.emitValue);
    };
    FormTable.prototype.handleSaveTableOrder = function (moved, rows) {
        var onChange = this.props.onChange;
        onChange(rows.map(function (item) { return (tslib.__assign({}, item)); }));
    };
    FormTable.prototype.handlePageChange = function (page) {
        this.setState({ page: page });
    };
    /**
     * Table Row中数据更新到InputTable中
     * 解决columns形如[{name: 'a'}, {name: 'c', value: '${a}'}]时，使用默认值的列数据无法更新到数据域的问题
     *
     * @param data 行数据
     * @param rowIndex 行索引值
     */
    FormTable.prototype.handlePristineChange = function (data, rowIndex) {
        var _this = this;
        var needConfirm = this.props.needConfirm;
        var index = Number(rowIndex);
        this.setState(function (prevState) {
            var items = cloneDeep__default["default"](prevState.items);
            if (Number.isInteger(index) &&
                inRange__default["default"](index, 0, items.length) &&
                !isEqual__default["default"](items[index], data)) {
                items.splice(index, 1, data);
                return { items: items };
            }
            return null;
        }, function () {
            if (needConfirm === false) {
                _this.emitValue();
            }
            else {
                Number.isInteger(index) && _this.startEdit(index, true);
            }
        });
    };
    FormTable.prototype.removeEntry = function (entry) {
        if (this.entries.has(entry)) {
            this.entries.delete(entry);
        }
    };
    FormTable.prototype.getEntryId = function (entry) {
        if (!this.entries.has(entry)) {
            this.entries.set(entry, this.entityId++);
        }
        return String(this.entries.get(entry));
    };
    FormTable.prototype.tableRef = function (ref) {
        var _a;
        while (ref && ref.getWrappedInstance) {
            ref = ref.getWrappedInstance();
        }
        this.tableStore = (_a = ref === null || ref === void 0 ? void 0 : ref.props) === null || _a === void 0 ? void 0 : _a.store;
    };
    FormTable.prototype.render = function () {
        var _this = this;
        var _a, _b;
        var _c = this.props, className = _c.className; _c.value; var showAddBtn = _c.showAddBtn, disabled = _c.disabled, render = _c.render, placeholder = _c.placeholder, draggable = _c.draggable, addable = _c.addable, columnsTogglable = _c.columnsTogglable, combineNum = _c.combineNum, combineFromIndex = _c.combineFromIndex, __ = _c.translate, canAccessSuperData = _c.canAccessSuperData, expandConfig = _c.expandConfig, affixRow = _c.affixRow, prefixRow = _c.prefixRow, formInited = _c.formInited, perPage = _c.perPage, cx = _c.classnames, rowClassName = _c.rowClassName, rowClassNameExpr = _c.rowClassNameExpr, _d = _c.affixHeader, affixHeader = _d === void 0 ? false : _d, _e = _c.autoFillHeight, autoFillHeight = _e === void 0 ? false : _e, tableContentClassName = _c.tableContentClassName;
        if (formInited === false) {
            return null;
        }
        var items = this.state.items;
        var showPager = false;
        var page = this.state.page || 1;
        var offset = 0;
        var lastPage = 1;
        if (typeof perPage === 'number' && perPage && items.length > perPage) {
            lastPage = Math.ceil(items.length / perPage);
            items = items.slice((page - 1) * perPage, page * perPage);
            showPager = true;
            offset = (page - 1) * perPage;
        }
        return (React__default["default"].createElement("div", { className: cx('InputTable', className) },
            render('body', {
                type: 'table',
                placeholder: __(placeholder),
                columns: this.state.columns,
                affixHeader: affixHeader,
                prefixRow: prefixRow,
                affixRow: affixRow,
                affixOffsetTop: (_b = (_a = this.props.affixOffsetTop) !== null && _a !== void 0 ? _a : this.props.env.affixOffsetTop) !== null && _b !== void 0 ? _b : 0,
                autoFillHeight: autoFillHeight,
                tableContentClassName: tableContentClassName
            }, {
                ref: this.tableRef.bind(this),
                value: undefined,
                saveImmediately: true,
                disabled: disabled,
                draggable: draggable && !~this.state.editIndex,
                items: items,
                getEntryId: this.getEntryId,
                onSave: this.handleTableSave,
                onSaveOrder: this.handleSaveTableOrder,
                buildItemProps: this.buildItemProps,
                quickEditFormRef: this.subFormRef,
                columnsTogglable: columnsTogglable,
                combineNum: combineNum,
                combineFromIndex: combineFromIndex,
                expandConfig: expandConfig,
                canAccessSuperData: canAccessSuperData,
                reUseRow: false,
                offset: offset,
                rowClassName: rowClassName,
                rowClassNameExpr: rowClassNameExpr
                // TODO: 这里是为了处理columns里使用value变量添加的，目前会影响初始化数据加载后的组件行为，先回滚
                // onPristineChange: this.handlePristineChange
            }),
            (addable && showAddBtn !== false) || showPager ? (React__default["default"].createElement("div", { className: cx('InputTable-toolbar') },
                addable && showAddBtn !== false ? (React__default["default"].createElement(amisUi.Button, { disabled: disabled, size: "sm", onClick: function () { return _this.addItem(_this.state.items.length); } },
                    React__default["default"].createElement(amisUi.Icon, { icon: "plus", className: "icon" }),
                    React__default["default"].createElement("span", null, __('add')))) : null,
                showPager
                    ? render('pager', {
                        type: 'pagination'
                    }, {
                        activePage: page,
                        lastPage: lastPage,
                        onPageChange: this.handlePageChange,
                        className: 'InputTable-pager'
                    })
                    : null)) : null));
    };
    FormTable.defaultProps = {
        placeholder: 'placeholder.empty',
        scaffold: {},
        addBtnIcon: 'plus',
        copyBtnIcon: 'copy',
        editBtnIcon: 'pencil',
        deleteBtnIcon: 'minus',
        confirmBtnIcon: 'check',
        cancelBtnIcon: 'close',
        valueField: ''
    };
    FormTable.propsList = [
        'onChange',
        'name',
        'columns',
        'label',
        'scaffold',
        'showAddBtn',
        'addable',
        'removable',
        'copyable',
        'editable',
        'addApi',
        'updateApi',
        'deleteApi',
        'needConfirm',
        'canAccessSuperData',
        'formStore'
    ];
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object, String]),
        tslib.__metadata("design:returntype", void 0)
    ], FormTable.prototype, "handlePristineChange", null);
    return FormTable;
}(React__default["default"].Component));
/** @class */ ((function (_super) {
    tslib.__extends(TableControlRenderer, _super);
    function TableControlRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    TableControlRenderer = tslib.__decorate([
        amisCore.FormItem({
            type: 'input-table'
        })
    ], TableControlRenderer);
    return TableControlRenderer;
})(FormTable));

exports["default"] = FormTable;
